package me.helllp.dake.generator.config.enums;

import me.helllp.dake.generator.meta.PomMetaData;
import org.apache.logging.log4j.util.Strings;

import java.util.ArrayList;
import java.util.List;

/**
 * 支持的数据库，目前仅支持MYSQL，其他数据湖后续会进行支持
 *
 * @author 林晓明
 */
public enum DbTypeEnum {
    DB2,
    MYSQL,
    SQLSERVER,
    ORACLE,
    POSTGRESQL,
    H2
    ;

    /**
     * 根据不同的数据库类型，构建依赖的JDBC驱动列表
     *
     * @return
     */
    public List<PomMetaData> makeDependency() {
        List<PomMetaData> list = new ArrayList<>();

        switch (this){
            case DB2:
                list.add(new PomMetaData("com.ibm.db2", "jcc", null));
                break;
            case MYSQL:
                list.add(new PomMetaData("mysql", "mysql-connector-java", null));
                break;
            case H2:
                list.add(new PomMetaData("com.h2database", "h2", null));
                break;
            case ORACLE:
                list.add(new PomMetaData("com.oracle.database.jdbc", "ojdbc-bom", null));
                break;
            case SQLSERVER:
                list.add(new PomMetaData("com.microsoft.sqlserver", "mssql-jdbc", null));
                break;
            case POSTGRESQL:
                list.add(new PomMetaData("org.postgresql", "postgresql", null));
                break;
        }

        return list;
    }

    /**
     * 根据不同的数据库类型，返回使用的驱动名称
     *
     * @return 驱动名称
     */
    public String makeDriverName() {
        String driver = Strings.EMPTY;

        switch (this){
            case DB2:
                driver = "COM.ibm.DB2.jdbc.net.DB2Driver";
                break;
            case MYSQL:
                driver = "com.mysql.cj.jdbc.Driver";
                break;
            case H2:
                driver = "org.h2.Driver";
                break;
            case ORACLE:
                driver = "oracle.jdbc.OracleDriver";
                break;
            case SQLSERVER:
                driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                break;
            case POSTGRESQL:
                driver = "org.postgresql.Driver";
                break;
        }
        return driver;
    }

    /**
     * 数据库连接的模板
     *
     * @return
     */
    public String makeUrlTemplate() {
        // TODO 当前仅支持mysql，需要增加其他支持
        return "jdbc:mysql://${hostName}:${port}/${catalog}?useSSL=true&nullCatalogMeansCurrent=true";
    }
}
